/**
 * 
 */
package com.hexacta.hrs.repositories.impl;

import java.util.GregorianCalendar;
import java.util.HashSet;

import javax.annotation.Resource;

import org.vectrics.company.Company;
import org.vectrics.company.Sector;
import org.vectrics.domain.Domain;
import org.vectrics.employee.Employee;
import org.vectrics.location.Locale;
import org.vectrics.location.Location;
import org.vectrics.user.Person;

/**
 * @author gvilla
 * 
 */
public class PersonFixture extends HibernateGenericDAOTest {

    @Resource(name = "employeeGenericRepository")
    EmployeeRecordObjectRepository employeeRepo = null;

    @Resource(name = "personGenericRepository")
    PersonRecordObjectRepository personRepo = null;

    private Locale locale;

    private Domain domain;

    private Location location;

    private Sector sector;

    public PersonFixture(final EmployeeRecordObjectRepository employeeRepo, final PersonRecordObjectRepository personRepo) {

        this.employeeRepo = employeeRepo;
        this.personRepo = personRepo;

    }

    public Person createValidPersistedPerson() {
        Person person = new Person();
        person.setLocale(createValidPersistedLocale());
        person.setBirthCountry(createValidPersistedLocation());
        person.setCreatedDate(new GregorianCalendar(2011, 6, 6).getTime());
        person.setLastModifiedDate(new GregorianCalendar(2012, 6, 6).getTime());
        person.setDomain(createValidPersistedDomain());
        person.setSystemUser(false);
        person.setVoided(false);
        personRepo.save(person);

        return person;
    }

    /**
     * @return
     */
    private Location createValidPersistedLocation() {
        if (location == null) {
            location = new Location();
            location.setName("aLocation");
            location.setTypeCode("aCode");
            location.setApproved(true);
            location.setVoided(false);
            personRepo.getSessionFactory().getCurrentSession().save(location);
        }
        return location;
    }

    /**
     * @return
     */
    private Locale createValidPersistedLocale() {
        if (locale == null) {
            Locale loc = new Locale();
            loc.setCode("AL");
            loc.setDescription("aLocale");
            personRepo.getSessionFactory().getCurrentSession().save(loc);
            locale = loc;
        }
        return locale;
    }

    /**
     * @param repo
     * @return
     */
    private Domain createValidPersistedDomain() {
        if (domain == null) {
            domain = new Domain();
            domain.setChildren(new HashSet<Domain>());
            domain.setDescription("aDomain");
            domain.setVoided(false);
            personRepo.getSessionFactory().getCurrentSession().save(domain);
        }
        return domain;
    }

    /**
     * @param employeeRepo
     * @param person1
     * @return
     */
    public Employee createValidPersistedEmployee(final Person person1) {

        Employee employee = new Employee();

        employee.setCompany(createValidPersistedCompany());
        employee.setPerson(person1);
        employee.setDateStart(new GregorianCalendar(2011, 6, 6).getTime());
        employee.setCreatedBy(createValidPersistedPerson());
        employee.setCreatedDate(new GregorianCalendar(2011, 6, 6).getTime());
        employee.setLastModifiedBy(createValidPersistedPerson());
        employee.setLastModifiedDate(new GregorianCalendar(2011, 6, 6).getTime());
        employee.setVoided(false);
        employee.setElegibleMentor(false);
        employee.setElegibleProjectMember(true);
        employeeRepo.save(employee);

        return employee;
    }

    /**
     * @param employeeRepo
     * @return
     */
    private Company createValidPersistedCompany() {

        Company company = new Company();

        company.setDomain(createValidPersistedDomain());
        company.setName("aCompany");
        company.setCreatedBy(createValidPersistedPerson());
        company.setCreatedDate(new GregorianCalendar(2011, 6, 6).getTime());
        company.setLastModifiedBy(createValidPersistedPerson());
        company.setLastModifiedDate(new GregorianCalendar(2012, 6, 6).getTime());
        company.setVoided(false);
        company.setSector(createValidPersistedSector());
        employeeRepo.getSessionFactory().getCurrentSession().save(company);

        return company;
    }

    /**
     * @return
     */
    private Sector createValidPersistedSector() {
        if (sector == null) {
            sector = new Sector();
            sector.setCode("SEC");
            sector.setDescriptionCode("aDescription");
            sector.setVoided(false);
            employeeRepo.getSessionFactory().getCurrentSession().save(sector);
        }

        return sector;
    }
}
